<?php
namespace app\api\controller;

use app\api\service\log\AdminOperateLogService;
use app\api\service\system\AdminUserService;
use app\BaseController;
use app\util\AdminUserUtil;
use app\util\CommonUtil;
use app\util\ResponseUtil;

class LoginAdmin extends BaseController
{
    public function submit()
    {
        try{
            $account = $this->requestData['account'] ?? '';
            $password = $this->requestData['password'] ?? '';
            if (!$account) {
                return ResponseUtil::failure("请输入账号");
            }
            if (!$password) {
                return ResponseUtil::failure("请输入密码");
            }
            $serviceAdminUser = new AdminUserService();
            $info = $serviceAdminUser->getAdminUserInfoByCondition(['account' => $account, 'status' => 1]);
            if (!$info) {
                return ResponseUtil::failure("账号不存在");
            }

            if(empty($info['role_id'])){
                return ResponseUtil::failure("该管理员还未分配角色");
            }

            $res = $serviceAdminUser->checkUserPassword($password, $info['passwd']);
            if (!$res) {
                return ResponseUtil::failure("密码不正确");
            }

            $data = [
                'account'  => $info['account'],
                'nickname' => $info['nickname'],
                'real_name' => $info['real_name'],
                'admin_user_id'  => $info['id'],
                'role_id'  => $info['role_id'],
            ];
            $token = AdminUserUtil::encodeUserInfo($data);
            $_SESSION['admin_login_info'] = json_encode($data);
            $logData = [
                'subject_id' => $data['admin_user_id'],
                'remark'  => $data['account'],
                'description' => $data['nickname']."登录",
            ];
            AdminUserUtil::addOperateLog('Login', AdminOperateLogService::ARR_ACTION_TYPE['login'], $logData);
            return ResponseUtil::success(['access_token' => $token], "登录成功");
        }catch(\Exception $e){
            CommonUtil::addErrorLog('Login', 'submit',  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("登录失败");
        }
    }


    /**
     * Desc: 前端用户登录
     * @return \think\response\Json
     * Datetime: 2024/12/13 8:08 by zdb
     */
    public function submitFront()
    {
        try{
            $account = $this->requestData['account'] ?? '';
            $password = $this->requestData['password'] ?? '';
            if (!$account) {
                return ResponseUtil::failure("请输入账号");
            }
            if (!$password) {
                return ResponseUtil::failure("请输入密码");
            }
            $serviceAdminUser = new AdminUserService();
            $info = $serviceAdminUser->getAdminUserInfoByCondition(['account' => $account, 'status' => 1]);
            if (!$info) {
                return ResponseUtil::failure("账号不存在");
            }

            if(empty($info['role_id'])){
                return ResponseUtil::failure("该管理员还未分配角色");
            }

            $res = $serviceAdminUser->checkUserPassword($password, $info['passwd']);
            if (!$res) {
                return ResponseUtil::failure("密码不正确");
            }

            $data = [
                'account'  => $info['account'],
                'nickname' => $info['nickname'],
                'real_name' => $info['real_name'],
                'admin_user_id'  => $info['id'],
                'role_id'  => $info['role_id'],
            ];
            $token = AdminUserUtil::encodeUserInfo($data);
            $_SESSION['admin_login_info'] = json_encode($data);
            $logData = [
                'subject_id' => $data['admin_user_id'],
                'remark'  => $data['account'],
                'description' => $data['nickname']."登录",
            ];
            AdminUserUtil::addOperateLog('Login', AdminOperateLogService::ARR_ACTION_TYPE['login'], $logData);
            return ResponseUtil::success(['access_token' => $token], "登录成功");
        }catch(\Exception $e){
            CommonUtil::addErrorLog('Login', 'submit',  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("登录失败");
        }
    }
}
